<?php

declare(strict_types=1);

namespace app\common\model;

class ExamineUser extends Base
{

    protected $name = 'examine_user';
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,rname,mobile');
    }
    /**
     * 添加考生
     */
    public static function addUser($param, $list)
    {
        try {
            // parent::destroy(['examine_id' => $param['id']], true);
            $ulist = parent::where(['examine_id'=>$param['id']])->column('uid');
            foreach ($list as $v) {
                $info = parent::where(['uid'=>$v,'examine_id'=>$param['id']])->find();
                if(empty($info)){
                    $data=[
                        'uid'=>$v,
                        'examine_id'=>$param['id'],
                        'cate'=>$param['cate'],
                    ];
                    parent::create($data);
                }else{
                    if(!in_array($v,$ulist)){
                        parent::destroy(['examine_id' => $param['id'],'uid'=>$v], true);
                    }
                }
            }
            return ['code'=>1,'msg'=>'操作成功'];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
